package com.sttk.secreconeclick.bookkeep.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.activity.ManageTypeActivity;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.bean.BillTypeBean;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ManageTypeAdapter extends RecyclerView.Adapter<ManageTypeAdapter.ViewHolder> {
    private List<BillTypeBean> mDatas;
    private boolean isIncome;

    public ManageTypeAdapter() {
        mDatas = new ArrayList<>();
    }

    public void setList(List<BillTypeBean> list) {
        this.mDatas = list;
        notifyDataSetChanged();
    }

    public void setIncome(boolean income) {
        isIncome = income;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_manage_type, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        BillTypeBean bean = mDatas.get(position);

        holder.tvName.setText(bean.getName());

        // 选中状态
        holder.ivIcon.setImageResource(bean.getIcon());

        // 编辑按钮点击事件
        holder.ivEdit.setOnClickListener(view -> {
            if (listener != null) {
                listener.clickEdit(position);
            }
        });

        // 删除按钮点击事件
        holder.ivDelete.setOnClickListener(view -> {
            if (listener != null) {
                listener.clickDelete(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    // 拖动排序逻辑
    public void onItemMove(int fromPos, int toPos) {
        Collections.swap(mDatas, fromPos, toPos);
        List<AccountBookBean> listSave = AppSpUtils.getAccountBookList();
        AccountBookBean accountBookBean = listSave.get(ManageTypeActivity.accountBookPos);
        if (isIncome) {
            accountBookBean.setIncomeTypeList(mDatas);
        } else {
            accountBookBean.setExpenditureTypeList(mDatas);
        }
        AppSpUtils.saveAccountBookList(listSave);
        notifyItemMoved(fromPos, toPos);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivIcon, ivEdit, ivDelete;
        TextView tvName;

        ViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivManageTypeItemImg);
            ivEdit = itemView.findViewById(R.id.ivManageTypeItemEdit);
            ivDelete = itemView.findViewById(R.id.ivManageTypeItemDelete);

            tvName = itemView.findViewById(R.id.tvManageTypeItemName);
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickEdit(int pos);

        void clickDelete(int pos);
    }
}
